{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取文件\n",
    "df_raw = pd.read_table('./实例文本.txt',sep=' ',header=None, encoding='gbk')\n",
    "df_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转置并删除空值\n",
    "df_raw = df_raw.T\n",
    "df_raw = df_raw.dropna(axis=0).reset_index()\n",
    "df_raw.drop(['index'], axis=1, inplace=True) \n",
    "df_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加辅助列\n",
    "def fuzhu(x):\n",
    "    if df_raw[df_raw[0] == x].index.tolist()[0] % 2 == 0:\n",
    "        return str(1)\n",
    "    else:\n",
    "        return str(2)\n",
    "\n",
    "df_raw['辅助列'] = df_raw.applymap(fuzhu)\n",
    "df_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分组合并\n",
    "df_group = df_raw.groupby('辅助列')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
